package com.bifang.common.licence;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.bifang.common.exception.AppException;
import com.bifang.common.vo.CreateLicenceVO;
import com.bifang.common.vo.LicenceVO;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;

public class LicenceUtil {
    public static LicenceVO getLinces() throws IOException {
        Properties properties = new Properties();
        try {
            ClassPathResource resource = new ClassPathResource("licence.properties");
            properties.load(resource.getStream());
        } catch (Exception e) {
            throw new AppException("读取核心配置文件Licence失败");
        }

        String osName = properties.getProperty("licence.os.name");
        String osVersion = properties.getProperty("licence.os.version");
        String osArch = properties.getProperty("licence.os.arch");
        String hostName = properties.getProperty("licence.host.name");
        String hostAddress = properties.getProperty("licence.host.address");
        String ip = properties.getProperty("licence.ip");
        String endTime = properties.getProperty("licence.validity.endTime");
        String hashCode = properties.getProperty("licence.hashCode");
        LicenceVO licenceVO = new LicenceVO();
        licenceVO.setEndTime(endTime);
        licenceVO.setHostAddress(hostAddress);
        licenceVO.setHostName(hostName);
        licenceVO.setOsArch(osArch);
        licenceVO.setOsName(osName);
        licenceVO.setOsVersion(osVersion);
        licenceVO.setHashCode(hashCode);
        return licenceVO;
    }
    /** 生成hashCode */
    public static String createHashCode(CreateLicenceVO licenceVO) throws IOException {
        if (StringUtils.isBlank(licenceVO.getEndTime())) {
            throw new AppException("有效期截止时间不能为空");
        }
        String hashCode = enc(JSONUtil.toJsonStr(licenceVO));
        return hashCode;
    }

    public static LicenceVO checkHashCode() throws IOException {
        LicenceVO licenceVO = getLinces();
        checkHashCode(licenceVO);
        return licenceVO;
    }

    public static void checkHashCode(LicenceVO licenceVO) throws IOException {
        String hashCode = licenceVO.getHashCode();
        CreateLicenceVO vo = new CreateLicenceVO();
        if (StringUtils.isNotBlank(licenceVO.getOsVersion())) {
            vo.setOsVersion(licenceVO.getOsVersion());
        }
        if (StringUtils.isNotBlank(licenceVO.getOsArch())) {
            vo.setOsArch(licenceVO.getOsArch());
        }
        if (StringUtils.isNotBlank(licenceVO.getOsName())) {
            vo.setOsName(licenceVO.getOsName());
        }
        if (StringUtils.isNotBlank(licenceVO.getHostAddress())) {
            vo.setHostAddress(licenceVO.getHostAddress());
        }
        if (StringUtils.isNotBlank(licenceVO.getHostName())) {
            vo.setHostName(licenceVO.getHostName());
        }
        if (StringUtils.isNotBlank(licenceVO.getEndTime())) {
            vo.setEndTime(licenceVO.getEndTime());
        }

        String checkCode = createHashCode(vo);
        if (!checkCode.equals(hashCode)) {
            throw new AppException("核心配置文件Licence失效，不能启动！！！请联系系统开发商重新生成！！！");
        }
    }

    public static String enc(String str) {
        return StringUtils.isBlank(str)
                ? str
                : SecureUtil.aes(Base64.decode("bGluY2Vuc1poQW5kaGhYeQ=="))
                        .encryptBase64(str, Charset.forName("UTF-8"));
    }

    public static String dec(String str) {
        try {
            return StringUtils.isBlank(str)
                    ? str
                    : SecureUtil.aes(Base64.decode("bGluY2Vuc1poQW5kaGhYeQ=="))
                            .decryptStr(str, Charset.forName("UTF-8"));
        } catch (Exception e) {
            return str;
        }
    }

    public static void main(String[] args) {
        CreateLicenceVO vo = new CreateLicenceVO();
        vo.setEndTime("20301231");
        try {
            System.out.println(LicenceUtil.createHashCode(vo));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
